package com.uniview.aspect;


import com.uniview.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;

@Aspect
@Component
@Slf4j
public class AuthAspect {
    private static final AntPathMatcher pathMatch = new AntPathMatcher();
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    Environment env;

    @Around("execution(* com..controller..*.*(..)) " +
            "&& (@annotation(org.springframework.web.bind.annotation.RequestMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.GetMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.PatchMapping))"
    )
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        try {
            MethodSignature signature= (MethodSignature) pjp.getSignature();
            Annotation[][] parameterAnnotations= signature.getMethod().getParameterAnnotations();
            Object[] args= pjp.getArgs();
            for (Annotation[] parameterAnnotation: parameterAnnotations) {
                int paramIndex = ArrayUtils.indexOf(parameterAnnotations, parameterAnnotation);
                for (Annotation annotation : parameterAnnotation) {
                    if (annotation instanceof RequestBody) {
                        Object paramValue = args[paramIndex];
                        System.out.println(paramValue);
                    }
                }
            }
            this.checkAuth();
            return pjp.proceed();
        }catch (CommonException e1){
            log.error(e1.getErrorMsg(),e1);
            throw new CommonException(e1.getErrorCode(),e1.getMessage());
        }catch (Throwable e) {
            throw e;
        }
    }

    public void checkAuth(){
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String uri = request.getRequestURI();
        String[] permitUrls = env.getProperty("base.auth.permit.url").split("\\,");
        //默认需要校验权限
        String isAuth ="0";
        for(String permitUrl:permitUrls){
            pathMatch.match(permitUrl, uri);
            //设置为放行
            isAuth="1";
        }
        if("0".equals(isAuth)){
            //执行鉴权逻辑

        }
    }

}